package com.roomaja.roomaja.controller;

import com.roomaja.roomaja.service.WenKuPageProcessor;
import com.roomaja.roomaja.tool.WenKuTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * 下载文档的controller层
 * Created by lzc on 2018/3/30.
 */

@RestController
@RequestMapping("/file")
public class DownLoadController {

    @Autowired
    WenKuPageProcessor wenKuPageProcessor;
    @Autowired
    HttpServletResponse response;

    //保存文件的根路径
    public static final String ROOT_PATH = System.getProperty("user.dir");
    public static final String WENKU_PATH = "/wenku_source";
    public static final String DOC_PATH = "/doc";
    public static final String PPT_PATH = "/ppt";
    public static final String PDF_PATH = "/pdf";
    public static final String TXT_PATH = "/txt";

    static File doc = null;
    static File pdf = null;
    static File ppt = null;
    static File txt = null;


    private static String Y = "";

    static{
        doc = new File(ROOT_PATH+WENKU_PATH+DOC_PATH);
        pdf = new File(ROOT_PATH+WENKU_PATH+PDF_PATH);
        ppt = new File(ROOT_PATH+WENKU_PATH+PPT_PATH);
        txt = new File(ROOT_PATH+WENKU_PATH+TXT_PATH);
    }


    /**
     * 下载文件到服务器
     * @param docId
     * @param fileType
     */
    @RequestMapping(value = "/download", method = RequestMethod.POST)
    public String downloadFile(@RequestParam("docId") String docId, @RequestParam("fileType") String fileType){
        StringBuffer stringbuffer = new StringBuffer();
        stringbuffer.append("https://wenku.baidu.com/view/").append(docId).append(".html?from=search");

        System.out.println("下载文档的url ：" + stringbuffer.toString());

        if ("DOC".equalsIgnoreCase(fileType)){
            WenKuTool.DOC(stringbuffer.toString());
        }else if("PDF".equalsIgnoreCase(fileType)){
            WenKuTool.PDF(stringbuffer.toString());
        }else if("TXT".equalsIgnoreCase(fileType)){
            WenKuTool.TXT(stringbuffer.toString());
        }else if ("PPT".equalsIgnoreCase(fileType)){
            WenKuTool.PPT(stringbuffer.toString());
        }

        return "success";
    }

    /**
     * 下载文件的接口
     * @param docId
     * @param fileType
     */
    @RequestMapping(value="/preDownload", method= RequestMethod.POST)
    public void perDownloadFile(@RequestParam("docId") String docId, @RequestParam("fileType") String fileType) throws IOException {
        if(wenKuPageProcessor.isEverDownload(docId)){// 曾经下载过,直接下载到本地
//            DownloadFileToLocal(docId, fileType);
        }else{// 没有下载过，先下载到服务器，再下载到本地
            downloadFile(docId, fileType);
//            DownloadFileToLocal(docId, fileType);
        }
    }

    /**
     * 下载文件到本地
     * @param docId
     * @param fileType
     */
    @RequestMapping(value = "/DownloadFileToLocal", method= RequestMethod.POST)
    public void DownloadFileToLocal(@RequestParam("docId") String docId, @RequestParam("fileType") String fileType) throws IOException {
        if ("DOC".equalsIgnoreCase(fileType)){

        }else if("PDF".equalsIgnoreCase(fileType)){

        }else if("TXT".equalsIgnoreCase(fileType)){

        }else if ("PPT".equalsIgnoreCase(fileType)){

        }
    }

}
